// index.js
// 获取应用实例
const app = getApp()
Page({
  data: {
    phone:'',
    code:'',
    initCountDown:app.globalData.initCountDown,//原始倒计时
    countDown:app.globalData.initCountDown,//倒计时
  },
  //绑定手机号动态
  getPhone(e){
    console.log(e.detail.value)
    this.setData({
      phone:e.detail.value
    })
  },
  //获取验证码
  onGetCode(){
    let phone = this.data.phone;
    let that = this;
    if(!phone || phone.length<=0){
      wx.showToast({
        title: "请输入手机号",
        icon: 'none',
        duration: 2000
      })
      return false
    }

    if (!(/^1[34578]\d{9}$/.test(phone))) {
      wx.showToast({
        title: "请输入正确的手机号",
        icon: 'none',
        duration: 2000
      })
      return false
    }
    this.onCodeDown()
    wx.request({
      url: app.globalData.baseUrl + '/api/register/getVerifyCode',
      method: 'GET',
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'token': app.globalData.token
      },
      data: {
        "phone": phone,
      },
      success: function(res) {    
        if(res.data.status){
          
        }      
        
      },
      fail: function() {
        wx.hideLoading()
        console.log("请求失败")
      }
    })
  },
  //验证阿妈发送中
  onCodeDown(){
    let that = this;
    let time = that.data.countDown;
    let setTime;
    clearInterval(setTime)
    time --;
    that.setData({
      countDown:time
    })
    setTime = setInterval(()=>{
      time --;
      if(time<=0){
        that.setData({
          countDown:that.data.initCountDown
        })
        clearInterval(setTime);
      }else {
        that.setData({
          countDown:time
        })
      }
      
    },1000)
  },
  onSavePhone(e){
    console.log(e)
    let phone = e.detail.value.phone;
    let verify_code = e.detail.value.verify_code;
    if(!phone || phone.length<=0){
      wx.showToast({
        title: "请输入手机号",
        icon: 'none',
        duration: 2000
      })
      return false
    }
    if(!verify_code || verify_code.length<=0){
      wx.showToast({
        title: "请输入验证码",
        icon: 'none',
        duration: 2000
      })
      return false
    }
    if (!(/^1[34578]\d{9}$/.test(phone))) {
      wx.showToast({
        title: "请输入正确的手机号",
        icon: 'none',
        duration: 2000
      })
      return false
    }
    wx.showLoading({
      title: '提交中',
      mask:true
    })
    wx.request({
      url: app.globalData.baseUrl + '/api/my/changePhone',
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'token': app.globalData.token
      },
      data: {
        "phone": phone,
        "verify_code": verify_code
      },
      success: function(res) {    
        wx.hideLoading()           
        wx.showToast({
          title: res.data.msg,
          icon: 'none',
          duration: 2000
        })
      },
      fail: function() {
        wx.hideLoading()
        console.log("请求失败")
      }
    })
  },
  onLoad() {
    
  },
  
})
